package yxy.game.pm2.module.friend;

import java.util.HashSet;
import java.util.Set;

import org.redisson.api.RSet;

import yxy.apple.util.redisson.RedisException;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.game.pm2.bean.module.PlayerModule;

abstract public class RelationList extends PlayerModule {
	// bean===========================================================================

	public RelationList(String playerCode) {
		this.setPlayerCode(playerCode);
	}

	// func===========================================================================

	public int size() {
		try {
			return touch().size();
		} catch (RedisException e) {
			e.printStackTrace();
		}
		return 0;
	}

	public Set<String> all() {
		try {
			return touch().readAll();
		} catch (RedisException e) {
			e.printStackTrace();
		}
		return new HashSet<>();
	}

	public void add(String playerCode) throws RedisException {
		touch().add(playerCode);
	}

	public void remove(String playerCode) throws RedisException {
		touch().remove(playerCode);
	}

	// redis==========================================================================

	abstract protected String listName();

	abstract protected String key();

	protected RSet<String> touch() throws RedisException {
		return RedisManager.client(redis()).getSet(key());
	}

	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key());
	}

}
